<?php
	namespace Home\Controller;
	use Think\Controller;

	class ManageController extends Controller {

		public function manage() {
			if (isset($_SESSION['uid'])) {
				// var_dump($_SESSION);
				$_SESSION['url'] = $_SERVER['PHP_SELF'];
				$condition = 1;
				$this->assign('username', $_SESSION['username']);
				$result = M('search')->select();
				if($result) {
					// var_dump($result);
					$this->assign('result', $result);
				}
				$whichTag = 3;
				// var_dump($result);
				$this->assign('whichTag', $whichTag);
				$this->assign('condition', $condition);
				$this->display();
			}
			else {
				$condition = 0;
				$this->error('您还未登录，正在跳转至系统首页...', U('Blast/blast'), 1);
			}

		}

		public function delete() {
			// var_dump($_POST);
			if(I('post.primerid')) {
				$where['primerid'] = I('post.primerid');
				$result = M('search')->where($where)->delete();
				if ($result) {
					// var_dump($result);
					shell_exec("python D:/wamp64/www" . __ROOT__ . "/Public/python/delete.py -id " . $where['primerid']);
					$this->redirect('Manage/manage', "", 3 , '<br /><h1>删除成功，3秒后跳转至Manage页面...</h1>');
				}
			}
		}

		public function edit() {
			if(I('post.primerid')) {
				// var_dump($_POST);
				// 用于定位修改的primer
				$where['primerid'] = I('post.primerid');
				// 用于修改的数据
				$data['gene'] = trim(I('post.gene'));
				$data['forwardprimer']  = strtoupper(trim(I('post.forwardprimer')));
				$data['reverseprimer']  = strtoupper(trim(I('post.reverseprimer')));
				$data['tm']  = trim(I('post.tm'));
				$data['productgc']  = trim(I('post.productgc'));
				$data['productlength']  = trim(I('post.productlength'));
				$data['uploaddate']  =  date('Y-m-d H:i:s', time());
				$data['location']  = trim(I('post.location'));
				$data['identifier']  = trim(I('post.identifier'));
				$data['describe']  = trim(I('post.describe'));

				// $result = M('search')->where($where)->find();
				$result = M('search')->where($where)->save($data);
				if($result) {
					shell_exec("python D:/wamp64/www" . __ROOT__ . "/Public/python/delete.py -id " . $where['primerid']);

					if (!($data['forwardprimer'] == '-')) {
						$pidForward = $where['primerid'] . '_forward';
						// shell_exec("rm -rf /var/www/");
						shell_exec("python D:/wamp64/www" . __ROOT__ . "/Public/python/edit.py -id " . $pidForward . " -gene " . $data['gene'] . " -fprimer " . $data['forwardprimer']);
						// var_dump("python D:/wamp64/www" . __ROOT__ . "/Public/python/edit.py -id " . $pidForward . " -gene " . $data['gene'] . " -fprimer " . $data['forwardprimer']);
					}

					if (!($data['reverseprimer'] == '-')) {
						$pidReverse = $where['primerid'] . '_reverse';
						shell_exec("python D:/wamp64/www" . __ROOT__ . "/Public/python/edit.py -id " . $pidReverse . " -gene " . $data['gene'] . " -fprimer " . $data['reverseprimer']);
						// var_dump("python D:/wamp64/www" . __ROOT__ . "/Public/python/edit.py -id " . $pidReverse . " -gene " . $data['gene'] . " -fprimer " . $data['reverseprimer']);
					}
					$this->redirect('Manage/manage', "", 3 , '<br /><h1>修改成功，3秒后跳转至Manage页面...</h1>');
				}
			}
		}

		public function check() {
			// var_dump($_POST);
			if (I('post.primerid')) {
				$where['primerid'] = I('post.primerid');
				$species = $where['species'] = I('post.species');
				$gene  = $where['gene'] = trim(I('post.gene'));
				$forwardprimer = $where['forwardprimer']  = strtoupper(trim(I('post.forwardprimer')));
				$reverseprimer = $where['reverseprimer']  = strtoupper(trim(I('post.reverseprimer')));
				$where['tm']  = trim(I('post.tm'));
				$where['productgc']  = trim(I('post.productgc'));
				$where['productlength']  = trim(I('post.productlength'));
				$where['location']  = trim(I('post.location'));
				$where['identifier']  = trim(I('post.identifier'));
				$where['describe']  = trim(I('post.describe'));

				$data['hasprimer'] = false;
				// $result = M('search')->where($where)->fetchSql(true)->find();
				// echo "$result";
				$url = __ROOT__;
				$result = M('search')->where($where)->select();
				if ($result) {
					$data['hasprimer'] = true;
					$url .= "/home/Search/uploadResult?species=$species&gene=$gene&forwardprimer=$forwardprimer&reverseprimer=$reverseprimer&result=" . base64_encode(serialize($result));
					$data['url'] = $url;
				}

				$this->ajaxReturn($data);
			}
		}
	}

?>
